import urllib.request
import urllib.parse
from http.client import HTTPResponse

# 1.创建请求对象，编码
url = "https://fanyi.baidu.com/sug"
params = {
    "kw": "中"
}
headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    # 'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Connection': 'keep-alive',
    'Content-Length': '12',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie': 'BIDUPSID=971E6103DF2938C88C3BD0B7AC81F1CB; PSTM=1707244662; newlogin=1; BDUSS=XpZT1FVWGhYd3ZFMncwVW55WmZmQUZ1eEVlRm5IVmYwQlVUSTdMR3BiV2cxd1ZtRVFBQUFBJCQAAAAAAAAAAAEAAACNt7Q0emR6aDAwMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKBK3mWgSt5lQ; BDUSS_BFESS=XpZT1FVWGhYd3ZFMncwVW55WmZmQUZ1eEVlRm5IVmYwQlVUSTdMR3BiV2cxd1ZtRVFBQUFBJCQAAAAAAAAAAAEAAACNt7Q0emR6aDAwMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKBK3mWgSt5lQ; BAIDUID=971E6103DF2938C88C3BD0B7AC81F1CB:SL=0:NR=10:FG=1; APPGUIDE_10_6_9=1; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; H_PS_PSSID=40171_40206_40212_40215_40244_40295_40291_40288_40285_40079_40364_40352_40381_40369_40317_40412_40415_40446; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1709341237; APPGUIDE_10_7_1=1; H_WISE_SIDS=110085_287174_295725_294756_298307_298887_299168_298227_282466_291448_299798_294385_300041_300155_295437_300255_299258_299832_281879_279016_268434_298964_301272_301097_295637; H_WISE_SIDS_BFESS=110085_287174_295725_294756_298307_298887_299168_298227_282466_291448_299798_294385_300041_300155_295437_300255_299258_299832_281879_279016_268434_298964_301272_301097_295637; BA_HECTOR=21ak8l25248g858k258kag25vagggc1ivd5uv1t; ZFY=gSn5QNsTPOQ2FzT7BzJMdN5t4BFfPSmkAj7M4LXtqro:C; BAIDUID_BFESS=971E6103DF2938C88C3BD0B7AC81F1CB:SL=0:NR=10:FG=1; delPer=0; PSINO=1; ariaDefaultTheme=undefined; RT="z=1&dm=baidu.com&si=98d64c1f-2638-42e9-89eb-7c5874fd0257&ss=ltv7xdud&sl=5&tt=1vi&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=le6&ul=2r4w&hd=2r4z"; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1710662053; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; BDRCVFR[tox4WRQ4-Km]=mk3SLVN4HKm; ab_sr=1.0.1_OTJjNzRkZmZiODQwMTY0ZDY0MjIxZWViODhlYjRlNTBjNjk0YjdlMWVjNmExYmMzNzE4ODlhOTYxYzIwNTU3ODk1MTEzN2UzYTQ0Nzk0YjhiMWMxNmYxOTk5NDg1YWQ5YjY5N2EzZjdlYjE1YjQyODFjNWIyMWQ2YTJlYTZmZWJiYmU4ZDhlMDYwNTJiOTgyNjdlNzEzODNmOTQ4YWIxMDE0YTVkMmIyZmE2YWE2ZTY2NjU1MWY4NWU1ZjI1ZDhiNmNhNDU0NWFmYzA4NzY4MmVhMzVkNGYwMjg0NDJlM2M=',
    'Host': 'fanyi.baidu.com',
    'Origin': 'https://fanyi.baidu.com',
    'Referer': 'https://fanyi.baidu.com/?aldtype=16047&ext_channel=Aldtype',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Microsoft Edge";v="122"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"'
}
request = urllib.request.Request(url, headers=headers, data=urllib.parse.urlencode(params).encode("utf8"))
# 2.发送请求,返回响应对象
# response = urllib.request.urlopen(request)
opener = urllib.request.build_opener()
response = opener.open(request)  # type:HTTPResponse

# 3.读取数据，解码
print(response.read().decode("utf8"))
